package com.groupsix.controller;

import com.groupsix.entity.Dormitoryinfo;
import com.groupsix.service.DormitoryinfoService;
import com.groupsix.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 董盛昌
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 宿舍信息控制器
 * @date 2021/10/18 0:19
 */
@Controller
public class DormitoryinfoController {

    // 依赖注入
    @Autowired
    private DormitoryinfoService dormitoryinfoService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findDormitoryinfo.action")
    public String findDormitory(String aName,Integer sDormitoryid,String dDormbuilding,
                                Integer pageIndex, Integer pageSize, Model model) {

        PageInfo di = dormitoryinfoService.findPageInfo(aName,sDormitoryid,
                dDormbuilding,pageIndex,pageSize);
        model.addAttribute("di",di);
        return "dormitoryinfo_list";
    }

    /**
     * 添加宿舍信息
     */
    @RequestMapping("/addDormitoryinfo.action")
    public String addDormitory( @RequestBody Dormitoryinfo dormitoryinfo) {
        int d = dormitoryinfoService.addDormitoryinfo(dormitoryinfo);
        return "dormitoryinfo_list";
    }

    /**
     * 删除宿舍信息
     */
    @RequestMapping( "/deleteDormitoryinfo.action")
    @ResponseBody
    public String deleteDormitory(Integer dId) {
        int d = dormitoryinfoService.deleteDormitoryinfo(dId);
        return "dormitoryinfo_list";
    }

    /**
     * 修改学生信息
     */
    @RequestMapping( "/updateDormitoryinfo.action")
    public String updateDormitory( Dormitoryinfo dormitoryinfo) {
        int d = dormitoryinfoService.updateDormitoryinfo(dormitoryinfo);
        return "redirect:/findDormitoryinfo.action";
    }


    @RequestMapping( "/findDormitoryinfoById.action")
    public String findDormitoryById(Integer dId, HttpSession session) {

        Dormitoryinfo d= dormitoryinfoService.findDormitoryinfoById(dId);
        session.setAttribute("d",d);
        return "dormitoryinfo_edit";
    }

    /**
     * 宿舍人员信息查询
     */
    @RequestMapping("/findDormitoryinfoStudent.action")
    public String findDormitoryStudent(Dormitoryinfo dormitoryinfo,Model model) {
        List<Dormitoryinfo> d = dormitoryinfoService.findDormitoryinfoStudent(dormitoryinfo);
        model.addAttribute("ds",d);
        return "dormitory_Studentlist";
    }
}
